﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using AthCommander.Model.Shell;

namespace AthCommander.Model
{
    public class FileView
    {
        private string _path;

        #region Constructors

        public FileView(FileSystemInfo fileInfo)
        {
            SetState(fileInfo);
        }

        public FileView(string path)
            : this(new FileInfo(path))
        {
        }

        #endregion

        #region Name

        private string _name;
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }

        #endregion

        #region FullName

        public string FullName
        {
            get { return _path; }
        }

        #endregion

        #region IsDirectory

        public bool IsDirectory
        {
            get { return (_size == -1); }
        }

        #endregion

        #region Size

        private long _size;
        public long Size
        {
            get
            {
                return _size;
            }
        }

        #endregion

        #region FileModifyDates

        private DateTime _modified;
        public DateTime DateModified
        {
            get
            {
                return _modified;
            }
        }

        private DateTime _created;
        public DateTime DateCreated
        {
            get
            {
                return _created;
            }
        }

        #endregion

        #region Type

        private string _type;
        public string Type
        {
            get
            {
                return _type;
            }
        }

        #endregion

        #region Icon

        private Icon _icon;
        public Icon Icon
        {
            get
            {
                return _icon;
            }
        }

        #endregion

        #region PrivateHelpers

        private void SetState(FileSystemInfo fileInfo)
        {
            _path = fileInfo.FullName;
            _name = fileInfo.Name;

                if (fileInfo is FileInfo)
                {
                    _size = (fileInfo as FileInfo).Length;
                }
                else
                {
                    _size = -1;
                } 


            _modified = fileInfo.LastWriteTime;
            _created = fileInfo.CreationTime;

            ShellFileInfo info = ShellGetFileInfo.GetFileInfo(fileInfo.FullName);

            _type = info.szTypeName;

            // Pobieranie ikony
            _icon = System.Drawing.Icon.FromHandle(info.hIcon);
        }

        #endregion

        #region Methods

        public void Update()
        {
            SetState(new FileInfo(_path));
        }

        public void Update(FileSystemInfo fsi)
        {
            SetState(fsi);
        }

        #endregion
    }
}
